package com.atguigu.tingshu.album.mapper;

import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface TrackInfoMapper extends BaseMapper<TrackInfo> {


    IPage<TrackListVo> findUserTrackPage(Page<Object> page1, @Param("vo") TrackInfoQuery trackInfoQuery);
    @Update("update track_info set order_num=order_num-1 where track_info.order_num>#{orderNum} and track_info.album_id=#{albumId} and track_info.is_deleted=0")
    void updateOrderNum(@Param("albumId") Long albumId,@Param("orderNum") Integer orderNum);
    @Select("select ti.id trackId,\n" +
            "       ti.track_title,\n" +
            "       ti.media_duration,\n" +
            "       ti.order_num,\n" +
            "       ti.create_time createTime,\n" +
            "       max(if(ts.stat_type = '0701', ts.stat_num, 0)) playStatNum,\n" +
            "       max(if(ts.stat_type = '0703', ts.stat_num, 0)) commentStatNum\n" +
            "from track_info ti\n" +
            "         left join track_stat ts on ti.id = ts.track_id\n" +
            "where ti.album_id = #{albumId}\n" +
            "  and ti.is_open = 1\n" +
            "  and ti.is_deleted = 0\n" +
            "  and ti.status = '0501'\n" +
            "group by trackId")
    IPage<AlbumTrackListVo> selectAlbumTrackPage(Page<AlbumTrackListVo> page, @Param("albumId") Long albumId);
}
